// counterStore.js
import { shallowRef,shallowReactive } from 'vue'
import { defineStore } from 'pinia'
import FormItem from '@/components/vue-flow/nodes/formItem.vue'
import { PlusIcon, AlertCircleIcon, ChevronDown, MinusCircleIcon,Braces } from 'lucide-vue-next'
import { Textarea } from '@/components/ui/textarea'
import { Label } from '@/components/ui/label'
import { Input } from '@/components/ui/input'
import { Switch } from '@/components/ui/switch'
import { Button } from '@/components/ui/button'
import Holder from '@/components/vue-flow/nodes/holder.vue'
import {selectReference} from '@/lib/dict'
import SelectComponent from '@/components/vue-flow/nodes/selectComponent.vue'
import {useOptionsStore}  from './OptionsStore'
const template = {
  "id": 'start_1',
  "type": "start",
  "data":{
    "title": "开始",
    "description": '请提供需要添加到提示词中的信息，这些信息可以被提示词引用。',
    "properties":{
      'json': {
        "icon": 'Braces',
        "size": 16,
        "color": 'rgb(77, 83, 232)',
      },
    },
    'labels': [
      {"type":'Label', "label": '名称', "key": 'name','class':'block text-xs text-gray-400 w-150'},
      {"type":'Label', "label": '类型', "key": 'type' ,'class':'block text-xs text-gray-400 w-180'},
      {"type":'Label', "label": '描述', "key": 'description' ,'class':'block text-xs text-gray-400 w-129'},
      {"type":'Label', "label": '是否必填', "key": 'isRequired','class':'block text-xs text-gray-400 text-center flex-1'},
    ],
    "output":[{
				"name": "BOT_USER_INPUT",
				"type": "1",
        "description":'BOT_USER_INPUT',
        "required": false,
			}]
  }
}


export const useStartStore = defineStore({
  id: 'useStartStore',
  state: () => ({
    template
  }),
  actions: {
    init(item:any){
      if(item){
        // item.data.output.forEach(item=>delete item.ref)
        this.template.id = item.id
        this.template.type = item.type
        this.template.data.output = [...item.data.output?.map(item=>({
          "name":item.name,
          "type":item.type,
          "description":item.description,
          "required": item.required,
        }))]
      }
    },
    add() {
        this.template.data.output.push(
          {
           "name": "",
            "type": "1",
            "description":'',
            "required": true,
          }
        )
    },
    deleteItem(index) {
      this.template.data.output.splice(index, 1);
    },
    updateValue(index,key,value){ //同步更新output
      // const originValue = this.template.data.output[index][key];
      this.template.data.output[index][key] = value;
      const optionsStore = useOptionsStore();
      optionsStore.selectOptions = {
        [`${this.template.data.title}`]:{
          groupName:`${this.template.data.title}`,
          id:`${this.template.data.title}-${index}`,
          Options:[
            {
              label:value,
              value:value
            }
          ]
        }
      }
    }
  }
})
